package com.caigou.modules.famous.ui.publish

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.famous.R
import com.caigou.modules.famous.adpter.PublishTypeItemAdapter
import com.caigou.modules.famous.bean.MyRecordItemBean
import com.caigou.modules.famous.bean.PublishItemBean
import com.caigou.modules.famous.databinding.ActivityFamousPublishBinding
import com.caigou.modules.famous.viewmodel.PublishViewModel
import com.caigou.modules.famous.widget.FamousTypeDialog
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/11
 */
@Route(path = RouterPath.Famous.PAGE_PUBLISH)
@AndroidEntryPoint
class FamousPublishActivity : BaseActivity() {
    private lateinit var binding: ActivityFamousPublishBinding

    private val publishViewModel: PublishViewModel by viewModels()

    @JvmField
    @Autowired(name = AppConstants.FAMOUS_TYPE)
    var famousType: Int = 1

    @JvmField
    @Autowired(name = AppConstants.FAMOUS_POST_TYPE)
    var famousPostType: Int = 0

    private val mAdapter: PublishTypeItemAdapter by lazy {
        PublishTypeItemAdapter(mutableListOf())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFamousPublishBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.famousPublishLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        binding.famousPublishSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchMyRecordList()
            }
        }

        binding.famousPublishRecyclerView.run {
            layoutManager = LinearLayoutManager(this@FamousPublishActivity)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@FamousPublishActivity))
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val item = mAdapter.getItem(position)
            val publishItemBean = PublishItemBean(0, item.dareTypeId, item.dareTypeName, item.period)
            when (item.dareTypeId) {
                14, 15, 16, 17, 20, 21, 22, 35 -> {
                    ARouter.getInstance().build(RouterPath.Famous.PAGE_PUBLISH_DETAIL2)
                        .withInt(AppConstants.FAMOUS_TYPE, famousType)
                        .withInt(AppConstants.FAMOUS_POST_TYPE, famousPostType)
                        .withObject(AppConstants.FAMOUS_PUBLISH_ITEM_BEAN, publishItemBean)
                        .navigation()
                    finish()
                }
                else -> {
                    ARouter.getInstance().build(RouterPath.Famous.PAGE_PUBLISH_DETAIL)
                        .withInt(AppConstants.FAMOUS_TYPE, famousType)
                        .withInt(AppConstants.FAMOUS_POST_TYPE, famousPostType)
                        .withObject(AppConstants.FAMOUS_PUBLISH_ITEM_BEAN, publishItemBean)
                        .navigation()
                    finish()
                }
            }
        }

        binding.famousPublishTvTitle.run {
            text = if (famousType == 1) {
                "挑战港彩名人堂"
            } else {
                "挑战澳彩名人堂"
            }
            setOnClickListener {
                val dialog = FamousTypeDialog(this@FamousPublishActivity, famousType)
                dialog.apply {
                    rgFamousType.setOnCheckedChangeListener { group, checkedId ->
                        famousType = when (checkedId) {
                            btnHkFamous.id -> 1
                            btnMacaoFamous.id -> 2
                            else -> 1
                        }
                        text = if (famousType == 1) {
                            "挑战港彩名人堂"
                        } else {
                            "挑战澳彩名人堂"
                        }
                        dismiss()
                    }
                    show()
                }
            }
        }

        binding.famousPublishTvBack.setOnClickListener {
            finish()
        }

        fetchMyRecordList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(publishViewModel.myRecordListLiveData, ::handeGetMyRecordList)
    }

    private fun handeGetMyRecordList(result: Resource<MutableList<MyRecordItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.famousPublishSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取我的战绩异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.famousPublishSwipeRefreshLayout.isRefreshing = false
                if (result.data == null) {
                    return
                }
                mAdapter.setList(result.data!!)
            }
        }
    }

    private fun fetchMyRecordList() {
        publishViewModel.getMyRecordList(famousType, AppBackgroundInfo.instance.userBean!!.userId)
    }

}